package org.duang.db.mongo.parser.decode;

import java.lang.reflect.Field;

import org.duang.db.annotation.Vo;
import org.duang.db.mongo.parser.DecodeConvetor;
import org.duang.kit.ObjectKit;
import org.duang.kit.ToolsKit;

import com.mongodb.DBObject;

public class VoDecoder extends Decoder {

	public VoDecoder(DBObject dbo, Field field) {
		super(dbo, field);
		Vo vo = field.getAnnotation(Vo.class);
		String name = vo.name();
		if(ToolsKit.isNotEmpty(name)){
			value = dbo.get(name);
		}
	}

	@Override
	public String getFieldName() {
		return field.getName();
	}

	@Override
	public Object getFieldValue() {
		return value;
	}

	@Override
	public boolean isNullField() {
		return null == value;
	}

	@Override
	public void setObj(Object obj) {
		Object valueObj = DecodeConvetor.convetor(field.getType(), (DBObject)value);
		ObjectKit.setField(obj, field, valueObj);
	}

}
